//
//  TransactionDetailViewController.m
//  NewPro
//
//  Created by RYX on 2018/11/23.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "TransactionDetailViewController.h"
#import "TransactionDetailCell.h"
#import "TransactionStore.h"
#import "MerchantList.h"
#import "MerchantsDetailModel.h"
#import "TradeDetailModel.h"

@interface TransactionDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (weak, nonatomic) IBOutlet UILabel *currentTimeLa;
@property (nonatomic,copy) NSArray *listArr;
@property (nonatomic, copy) NSString *seletime;


@end

@implementation TransactionDetailViewController

- (UIView *)headerView{
    
    if (!_headerView) {
        
        _headerView = [[[NSBundle mainBundle] loadNibNamed:@"TransactionDetailHeaderView" owner:self options:nil] objectAtIndex:0];
        if (_month) {
            if (_month.length > 0) {
                _currentTimeLa.text = _month;
            }
        }
    }
    
    return _headerView;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = 5.5;
        _tableView.sectionFooterHeight = 0.5;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.1)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf refresh];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
    }
    
    return _tableView;
}

- (void)refresh{
    
    TransactionStore *store = [[TransactionStore alloc] init];
    
    NSString *merchantNo = [MerchantList sharedInstance].selectedModel.merchantNo;
    NSString *month = [MerchantList sharedInstance].selectedModel.month;
    
    MJWeakSelf
    [store getTradeDetailListWithMerchantNo:merchantNo andMonth:month Success:^(NSArray *listArr) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.listArr = listArr;
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

- (void)GetSearchDataWith:(NSString *)time{
    TransactionStore *store = [[TransactionStore alloc] init];
    NSString *merchantNo = [MerchantList sharedInstance].selectedModel.merchantNo;
    MJWeakSelf
    [store getTradeDetailListWithMerchantNo:merchantNo andDetailTime:time Success:^(NSArray *listArr) {
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.listArr = listArr;
        [weakSelf.tableView reloadData];
    } Failure:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    
}


- (void)setUI{
    
    [self setNavBarWithTitle:@"明细详情"];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(1);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    
    [self.tableView.mj_header beginRefreshing];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TransactionDetailCell getHeight];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSArray *arr = self.listArr[section];
    
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TransactionDetailCell *cell = [TransactionDetailCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.listArr[indexPath.section][indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - 选择日期
- (IBAction)onSelectDateBtn:(UIButton *)sender {
    
    NSDate *minDate = [NSDate setYear:1978 month:3 day:12];
    NSDate *maxDate = [NSDate date];
    
    [BRDatePickerView showDatePickerWithTitle:@"选择日期" dateType:BRDatePickerModeYMD defaultSelValue:self.seletime minDate:minDate maxDate:maxDate isAutoSelect:YES themeColor:nil resultBlock:^(NSString *selectValue) {
        self.seletime = selectValue;
        self.currentTimeLa.text = [selectValue stringByReplacingOccurrencesOfString:@"-" withString:@"."];
        
    } cancelBlock:^{
        
        
    }];
}

#pragma mark - 查询
- (IBAction)onSearchBtn:(UIButton *)sender {
    if (self.currentTimeLa.text.length <= 0) {
        return;
    }
    if (self.currentTimeLa.text.length == self.month.length) {
        [self.tableView.mj_header beginRefreshing];
    }else{
         [self GetSearchDataWith:self.currentTimeLa.text];
    }
}

@end
